package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.LinkedList;

import quq.missq.Constants;
import quq.missq.MLog;
import quq.missq.R;
import quq.missq.activity.BigShootDetailActivity;
import quq.missq.activity.BigShootDetailMediaActivity;
import quq.missq.activity.TopicDetailActivity;
import quq.missq.beans.TaPageBean;
import quq.missq.beans.TopicBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.StringUtils;
import quq.missq.utils.VolleyTool;
import quq.missq.views.NoScrollGridView;

/**
 * Created by River on 8/18/15.
 */
public class TaPagerAdapter extends BaseAdapter{

    private LinkedList<TaPageBean.Result> results;
    private Context context;
    private int mTab;//0:大招，1：话题


    public TaPagerAdapter(Context context){
        this.context = context;
    }

    public void setData(LinkedList<TaPageBean.Result> results){
        this.results = results;
    }

    public LinkedList<TaPageBean.Result> getData(){
        return this.results;
    }

    @Override
    public int getCount() {
        return results.size();
    }

    @Override
    public Object getItem(int position) {
        return results.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final TaPageBean.Result result = results.get(position);
        //测试一下数据

        Holder holder;
        if(convertView == null){
            holder = new Holder();
            convertView = LayoutInflater.from(context).inflate(R.layout.ta_page_adapter,null);
            holder.time = (TextView)convertView.findViewById(R.id.time);
            holder.gridView = (NoScrollGridView)convertView.findViewById(R.id.gridView);
            holder.avatorLayout1 = (RelativeLayout)convertView.findViewById(R.id.avtarLayout1);
            holder.avatorLayout2 = (RelativeLayout)convertView.findViewById(R.id.avtarLayout2);
            holder.avatorLayout3 = (RelativeLayout)convertView.findViewById(R.id.avtarLayout3);
            holder.avtar1 = (NetworkImageView)convertView.findViewById(R.id.avtar1);
            holder.avtar2 = (NetworkImageView)convertView.findViewById(R.id.avtar2);
            holder.avtar3 = (NetworkImageView)convertView.findViewById(R.id.avtar3);
            holder.joinCount = (TextView)convertView.findViewById(R.id.joinCount);
            holder.comment = (TextView)convertView.findViewById(R.id.comment);
            holder.content = (TextView)convertView.findViewById(R.id.content);
            holder.typeIcon = (ImageView)convertView.findViewById(R.id.topicType);
            holder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
            holder.commentLayout = (LinearLayout)convertView.findViewById(R.id.commentLayout);
            holder.commentLayout1 = (LinearLayout)convertView.findViewById(R.id.commentLayout1);
            holder.tsType = (ImageView)convertView.findViewById(R.id.tsType);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
        String friendly_time = StringUtils.friendly_time(result.getCreated());
        holder.time.setText(friendly_time);
        // 当category=123的时候 用一张图 其他的用gridview
        int categoryId = result.getPost().getCategory().getId();
        if(categoryId == 1 || categoryId == 2 || categoryId == 3){
            holder.tsType.setImageResource(R.mipmap.ts_dazhao);
            holder.imageView.setVisibility(View.VISIBLE);
            holder.gridView.setVisibility(View.GONE);
            ImageLoadUtil.loadImage(holder.imageView,Constants.IMAGE_HOST+ result.getPost().getAttachs().get(0).getThumbnail(),R.mipmap.empty_photo);
        }else{
            holder.tsType.setImageResource(R.mipmap.ts_huati);
            holder.imageView.setVisibility(View.GONE);
            holder.gridView.setVisibility(View.VISIBLE);
            if(result.getPost().getAttachs().size() > 0){
                holder.gridView.setAdapter(new ImageGridViewAdapter(context, result.getPost().getAttachs()));
            }
        }
        holder.content.setText(result.getPost().getTitle());
        int type = -1;
        if(categoryId == 3){
            type = R.mipmap.icon_plan;
        }else if(categoryId == 2){
            type = R.mipmap.icon_change;
        }else if(categoryId == 1){
            type = R.mipmap.icon_night;
        }else if(categoryId == 5){//话题类型显示有误
            type = R.mipmap.icon_qa;
        }else{
            type = R.mipmap.icon_show;
        }
        holder.typeIcon.setImageResource(type);
        showComments(holder, result.getPost().getDigestComments());
//        holder.gridView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    Intent intent = new Intent();
//                    intent.setClass(context, TopicDetailActivity.class);
//                    intent.putExtra("id", result.getPost().getId());
//                    context.startActivity(intent);
//                    return false;
//                }
//                return false;
//            }
//        });
        //测试
        final int mediaType = result.getPost().getMediaType();
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                mTab = result.getTab();//0:大招，1：话题
                MLog.e(mTab+"//"+position);
                if (mTab == 0){

                    if(mediaType == 0){
                        intent.setClass(context, BigShootDetailActivity.class);
                    }else{
                        intent.putExtra("headTitle", result.getPost().getTitle());
                        intent.putExtra("path", result.getPost().getMediaPath());
                        intent.setClass(context, BigShootDetailMediaActivity.class);
                    }
                    intent.putExtra("id", result.getPost().getId());
                }else{
                    intent.setClass(context, TopicDetailActivity.class);
                    intent.putExtra("id", result.getPost().getId());
                }
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    private static final String JOIN_COUNT = "人参与了此问答";
    private void showComments(Holder holder,ArrayList<TopicBean.DigestComment> comments){
        int counts = comments.size();
        String comment = "";
        if(counts == 0){
            holder.avatorLayout1.setVisibility(View.GONE);
            holder.avatorLayout2.setVisibility(View.GONE);
            holder.avatorLayout3.setVisibility(View.GONE);
        }
        if(counts == 1){
            comment = comments.get(0).getContent();
            showLayout1(holder,comments);
            holder.avatorLayout2.setVisibility(View.GONE);
            holder.avatorLayout3.setVisibility(View.GONE);
        }else if(counts == 2){
            comment = comments.get(0).getContent();
            showLayout1(holder,comments);
            showLayout2(holder,comments);
            holder.avatorLayout3.setVisibility(View.GONE);
        }else if(counts == 3){
            comment = comments.get(0).getContent();
            showLayout1(holder,comments);
            showLayout2(holder,comments);
            showLayout3(holder,comments);
        }
        holder.comment.setText(comment);
        if(counts > 0){
            holder.commentLayout1.setVisibility(View.VISIBLE);
            holder.commentLayout.setVisibility(View.VISIBLE);
            holder.joinCount.setVisibility(View.VISIBLE);
            holder.joinCount.setText(counts+JOIN_COUNT);
        }else{
            holder.commentLayout1.setVisibility(View.GONE);
            holder.commentLayout.setVisibility(View.GONE);
            holder.joinCount.setVisibility(View.GONE);
        }

    }

    private void showLayout1(Holder holder,ArrayList<TopicBean.DigestComment> comments){
        holder.avatorLayout1.setVisibility(View.VISIBLE);
//        holder.avtar1.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar1.setImageUrl(Constants.IMAGE_HOST+comments.get(0).getAuthor().getAvatar80(), VolleyTool.getImageLoader());
    }

    private void showLayout2(Holder holder,ArrayList<TopicBean.DigestComment> comments){
        holder.avatorLayout2.setVisibility(View.VISIBLE);
//        holder.avtar2.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar2.setImageUrl(Constants.IMAGE_HOST+comments.get(1).getAuthor().getAvatar80(),VolleyTool.getImageLoader());
    }

    private void showLayout3(Holder holder,ArrayList<TopicBean.DigestComment> comments){
        holder.avatorLayout3.setVisibility(View.VISIBLE);
//        holder.avtar3.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar3.setImageUrl(Constants.IMAGE_HOST+comments.get(2).getAuthor().getAvatar80(),VolleyTool.getImageLoader());
    }


    private class Holder{
        TextView time;
        NoScrollGridView gridView;
        RelativeLayout avatorLayout1;
        RelativeLayout avatorLayout2;
        RelativeLayout avatorLayout3;
        NetworkImageView avtar1;
        NetworkImageView avtar2;
        NetworkImageView avtar3;
        TextView joinCount;
        TextView comment;
        TextView content;
        ImageView typeIcon;
        ImageView imageView;
        ImageView tsType;
        LinearLayout commentLayout;
        LinearLayout commentLayout1;
    }

}
